package com.gitee.feizns.webtemplate.entity.vo.commons.result;

import java.util.HashMap;

/**
 * @author feizns
 * @since 2019/5/10
 */
public class Result<T> extends HashMap<String, Object> {

    private Result() {}

    public static Result ok() {
        return create()
                .put("success", true)
                .put("msg", "ok");
    }

    /**
     *
     * @param data
     * @param <T> 为了标识响应数据的结构，{data:{??}}
     * @return
     */
    public static <T> Result<T> ok(T data) {
        return ok().put("data", data);
    }

    /**
     *
     * @return
     */
    public static Result error() {
        return error("unknown");
    }

    public static Result error(String msg) {
        return create()
                .put("success", false)
                .put("msg", msg);
    }

    public static Result error(String msg, Object data) {
        return create()
                .put("success", false)
                .put("msg", msg)
                .put("data", data);
    }

    private static Result create() {
        return new Result();
    }

    @Override
    public Result put(String key, Object value) {
        super.put(key, value);
        return this;
    }

}
